package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.ui.MenuItem;
import java.util.Vector;
import javax.microedition.media.Player;

import com.sound.ampache.objects.Song;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;


class SongsScreen extends ItemsScreen implements HTTPTransportListener {
	
	private Song[] songs;
	private SongsListField songsList;
	public String screenTitle = "Songs";
	public Player myPlayer;
     
    	private MenuItem addSongMenu = new MenuItem("Add Song to Playlist",100,10) {
    		public void run() {
    			addSong();
    		} 
    	};
    	
    	private MenuItem downloadSongMenu = new MenuItem("Download Song",100,10) {
    		public void run() {
    			downloadSong();
    		} 
    	};
    
	SongsScreen()
	{		
		songsList = new SongsListField();
		ItemsScreen.itemsList = songsList;
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        songsList.setEmptyString("No Songs", DrawStyle.HCENTER);
        this.add(songsList);  	
	}
	
	
	public void getSongs(){
		getSongs("songs", "");
	}
	
	
	public void getSongs(String filter){
		getSongs("songs", filter);
	}
	
	
	public void getSongs(String method, String filter){
		setTitle(screenTitle);
		songsList.set(null); 
		statusDlg.show();
	    ampacheberry.comm.setHTTPTransportListener(this);
	    ampacheberry.comm.send(method, filter); 
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
        menu.add(addSongMenu);
        //menu.add(downloadSongMenu);
        menu.add(MenuItem.separator(addSongMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }

    
    private class SongsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/song.png");  
        
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                    
            int offsetY = (this.getRowHeight() - icon.getHeight())/2;
            graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
                    icon.getHeight(),icon,0,0);
            graphics.drawText("Song: " + songs[index].name, 
            		icon.getWidth() + 2, y + 1, DrawStyle.ELLIPSIS,
            		width - icon.getWidth() + 2);
            graphics.drawText("Artist: " + songs[index].artist, 
                    icon.getWidth() + 2, y + fontSize + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawText("Album: " + songs[index].album, 
                    icon.getWidth() + 2, y + (2 * fontSize) + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawLine(0, y + rowHeight - 1, listField.getWidth(),
            		y + rowHeight - 1);
        }
    };
	
    
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
                statusDlg.close();
    			songs = new Song[data.size()];
    			data.copyInto(songs);
    			songsList.set(songs);
            }
        });
	}

    
    private void addSong() {
    	int selected = songsList.getSelectedIndex();
    	ampacheberry.playlistSong.addSongToPlaylist(songs[selected]);	
    }
    
    
    private void downloadSong() {
    	    

    }
    
} 
